-
Notifications
You must be signed in to change notification settings - Fork 704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce TextFormatter allocations #3991
Reduce TextFormatter allocations #3991
Conversation
Also made the helper methods internal so they can be accessed from the test project.
Uses StringBuilder and char span indexof search to reduce intermediate allocations. The new implementation behaves slightly different compared to old implementation. In synthetic LFCR scenario it is correctly removed while the old implementation left the CR, which seems like an off-by-one error.
Almost identical to the StripCRLF implementation.
Appends rune chars to StringBuilder avoiding intermediate string allocation for each rune append.
…ith StringBuilder fallback
Uses stackalloc char buffer with fallback to rented array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently document reformat caused a bunch of indentation changes. I recommend ignoring whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noble and excellent work. Thank you!
FWIW, see #3617
Yeah, this was very much a bandage optimization. 😄 |
Reduces intermediate allocations in the simplest top of the chart TextFormatter helper methods, and StringExtensions.ToString(IEnumerable<Rune>), which is used everywhere.
Fixes
Proposed Changes
Pull Request Checklist
CTRL-K-D
to automatically reformat your files before committing.dotnet test
before commit///
style comments)Microbenchmarks
StringExtensions.ToString(IEnumerable<Rune>)
Note that this only benchmarks the non-StringBuilder-fallback side. Most of the call sites use Rune[] or List<Rune> anyways so the fallback is not that relevant. Might add benchmarks for the fallback later.
The input lengths are a bit overkill but you get the idea.
TextFormatter.RemoveHotKeySpecifier
TextFormatter.ReplaceCRLFWithSpace
TextFormatter.StripCRLF
Allocations
Overall nice reduction in string allocations. Naturally StringBuilder (and its internal char[]) allocations increased a bit because they are now used in ReplaceCRLFWithSpace and StripCRLF.